package com.hyy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyy.entity.User;
import com.hyy.mapper.UserMapper;
import com.hyy.service.UserService;
import com.hyy.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectAllUser(){
        return userMapper.selectList(null);
    }

    @Override
    public ResultData<User> selectlogin(User user) {
        //构建查询的构造器
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        lambdaQueryWrapper.eq(User::getPassword, user.getPassword());
        User u = userMapper.selectOne(lambdaQueryWrapper);
        if (u != null) {
            return ResultData.success("登录成功",u);
        }
        return ResultData.error(400,"用户名或者密码不正确");
    }

    @Override
    public ResultData<User> selectByUid(Integer uid) {
        User user = userMapper.selectById(uid);
        if (user != null) {
            return ResultData.success("查询成功",user);
        }
        return ResultData.error(400,"查询失败");
    }

    @Override
    public ResultData<User> selectPageUser(Integer currentPage, Integer pageSize) {
//        构建分页参数查询
        Page<User> page = new Page<>(currentPage, pageSize);
        Page<User> p = userMapper.selectPage(page,null);
        if(p!= null) {
            return ResultData.success("成功", p);
        }
        return ResultData.error(400,"失败");
    }

    @Override
    public ResultData<Integer> updateUserById(User user) {
        int num = userMapper.updateById(user);
        return ResultData.success(num);
    }

    @Override
    public ResultData<Integer> deleteUserByUid(Integer uid) {
        return ResultData.success(userMapper.deleteById(uid));
    }
}
